#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    setWindowIcon(QIcon("logo.ico"));
}

Widget::~Widget()
{
    delete ui;
}
void Widget::SetText(QString str){
    ui->label->setText(str);
}

void Widget::SetText1(QString str){
    ui->label_1->setText(str);
}

void Widget::init(){
    if(GameProcessID != 0){
        GameProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GameProcessID);
        //qDebug("please wait=====%d", GameProcessID);
        ModuleHandle = Szy_GetModuleHandleW(GameProcessHandle, L"System.Linq.dll");
        //qDebug("please wait=====%s", ModuleHandle);
    }
}

HMODULE Widget::Szy_GetModuleHandleW(HANDLE ProcessHandle, const wchar_t* ModuleName)
{
    HMODULE ModuleHandle[1024];
    memset(&ModuleHandle,0,sizeof(ModuleHandle));
    DWORD cbNeeded{0};
    /*if (!K32EnumProcessModulesEx(ProcessHandle, ModuleHandle, sizeof(ModuleHandle), &cbNeeded, LIST_MODULES_ALL)) {
        DWORD error = GetLastError();
        qDebug("Error: %lu\n", error);
    }*/

    if (K32EnumProcessModulesEx(ProcessHandle, ModuleHandle, sizeof(ModuleHandle), &cbNeeded, LIST_MODULES_ALL))
    {
        //qDebug("aaaaaaa=======");
        for (DWORD i = 0; i < (cbNeeded / sizeof(HMODULE)); i++)
        {
            wchar_t szModName[MAX_PATH];
            K32GetModuleBaseNameW(ProcessHandle, ModuleHandle[i], szModName, sizeof(szModName) / sizeof(TCHAR));
            if (wcscmp(szModName, ModuleName) == 0)
            {
                return ModuleHandle[i];
            }
        }
    }
    return 0;
}

DWORD Widget::Szy_GetProcessIDW(const wchar_t* ProcessName)
{
    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    PROCESSENTRY32W ProcessTable;
    memset(&ProcessTable,0,sizeof(PROCESSENTRY32W));
    ProcessTable.dwSize = sizeof(PROCESSENTRY32);
    bool rt = Process32First(hSnapshot, &ProcessTable);
    if (wcscmp(ProcessTable.szExeFile, ProcessName) == 0) {
        return ProcessTable.th32ProcessID;
    }
    while (rt)
    {
        rt = Process32Next(hSnapshot, &ProcessTable);
        if (wcscmp(ProcessTable.szExeFile, ProcessName) == 0) {
            return ProcessTable.th32ProcessID;
        }
    }
    return 0;
}

// 查找特征码的函数，返回一个地址
uintptr_t Widget::FindPattern(HANDLE hProcess, uintptr_t start, uintptr_t end, const std::vector<BYTE>& pattern, const std::string& mask) {
    MEMORY_BASIC_INFORMATION mbi;
    uintptr_t currentAddress = start;

    while (currentAddress < end) {
        // 查询当前内存区域的信息
        if (VirtualQueryEx(hProcess, (LPCVOID)currentAddress, &mbi, sizeof(mbi)) &&
            mbi.State == MEM_COMMIT && (mbi.Protect & (PAGE_EXECUTE_READWRITE | PAGE_READWRITE | PAGE_READONLY))) {

            std::vector<BYTE> buffer(mbi.RegionSize);
            SIZE_T bytesRead;

            // 读取内存
            if (ReadProcessMemory(hProcess, (LPCVOID)currentAddress, buffer.data(), buffer.size(), &bytesRead)) {
                for (size_t i = 0; i < bytesRead - pattern.size(); ++i) {
                    bool found = true;
                    for (size_t j = 0; j < pattern.size(); ++j) {
                        if (mask[j] != '?' && buffer[i + j] != pattern[j]) {
                            found = false;
                            break;
                        }
                    }
                    if (found) {
                        return currentAddress + i; // 找到匹配地址，立即返回
                    }
                }
            }
        }
        currentAddress += mbi.RegionSize;
    }

    return 0; // 如果没有找到匹配地址，返回0
}


// 查找特征码的函数，支持返回多个地址
std::vector<uintptr_t> Widget::FindPatterns(HANDLE hProcess, uintptr_t start, uintptr_t end, const std::vector<BYTE>& pattern, const std::string& mask) {
    MEMORY_BASIC_INFORMATION mbi;
    uintptr_t currentAddress = start;
    std::vector<uintptr_t> foundAddresses;

    while (currentAddress < end) {
        // 查询当前内存区域的信息
        if (VirtualQueryEx(hProcess, (LPCVOID)currentAddress, &mbi, sizeof(mbi)) &&
            mbi.State == MEM_COMMIT && (mbi.Protect & (PAGE_EXECUTE_READWRITE | PAGE_READWRITE | PAGE_READONLY))) {

            std::vector<BYTE> buffer(mbi.RegionSize);
            SIZE_T bytesRead;

            // 读取内存
            if (ReadProcessMemory(hProcess, (LPCVOID)currentAddress, buffer.data(), buffer.size(), &bytesRead)) {
                for (size_t i = 0; i < bytesRead - pattern.size(); ++i) {
                    bool found = true;
                    for (size_t j = 0; j < pattern.size(); ++j) {
                        if (mask[j] != '?' && buffer[i + j] != pattern[j]) {
                            found = false;
                            break;
                        }
                    }
                    if (found) {
                        foundAddresses.push_back(currentAddress + i); // 保存地址
                    }
                }
            }
        }
        currentAddress += mbi.RegionSize;
    }

    return foundAddresses; // 返回所有匹配地址
}


void Widget::setGameProcessID(DWORD ProcessID, HMODULE moduleHandle){
    if(ProcessID != 0){
        if(ProcessID != GameProcessID || moduleHandle != ModuleHandle){
            GameProcessID = ProcessID;
            GameProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GameProcessID);//取游戏进程句柄
            ModuleHandle = Szy_GetModuleHandleW(GameProcessHandle, L"System.Linq.dll"); //取游戏模块句柄
        }
        if(wxyg){
            DWORD64 tempYGAddr = 0; //临时阳光地址

            ReadProcessMemory(GameProcessHandle, (LPVOID)((DWORD64)(ModuleHandle) + 0x00077238), &tempYGAddr, sizeof(DWORD64), 0); //一级偏移
            //qDebug("please wait=====%d", tempYGAddr);
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x10), &tempYGAddr, sizeof(DWORD64), 0);//二级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x1FC), &tempYGAddr, sizeof(DWORD64), 0);//三级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x0), &tempYGAddr, sizeof(DWORD64), 0);//四级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x2C0), &tempYGAddr, sizeof(DWORD64), 0);//五级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x8), &tempYGAddr, sizeof(DWORD64), 0);//六级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x498), &tempYGAddr, sizeof(DWORD64), 0);//七级偏移
            int WriteYG = 99999999; //写入阳光数量
            WriteProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x624), &WriteYG, sizeof(WriteYG), 0);
        }
        if(wxtl){
            DWORD64 tempYGAddr = 0; //临时体力地址

            ReadProcessMemory(GameProcessHandle, (LPVOID)((DWORD64)(ModuleHandle) + 0x00077238), &tempYGAddr, sizeof(DWORD64), 0); //一级偏移
            //qDebug("please wait=====%d", tempYGAddr);
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x10), &tempYGAddr, sizeof(DWORD64), 0);//二级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x1FC), &tempYGAddr, sizeof(DWORD64), 0);//三级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x0), &tempYGAddr, sizeof(DWORD64), 0);//四级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0xC0), &tempYGAddr, sizeof(DWORD64), 0);//五级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0xA0), &tempYGAddr, sizeof(DWORD64), 0);//六级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x368), &tempYGAddr, sizeof(DWORD64), 0);//七级偏移
            int WriteYG = 1132920832; //写入体力数量
            WriteProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x77C), &WriteYG, sizeof(WriteYG), 0);
        }
        if(wxhp){
            DWORD64 tempYGAddr = 0; //临时生命地址

            ReadProcessMemory(GameProcessHandle, (LPVOID)((DWORD64)(ModuleHandle) + 0x00077238), &tempYGAddr, sizeof(DWORD64), 0); //一级偏移
            //qDebug("please wait=====%d", tempYGAddr);
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x10), &tempYGAddr, sizeof(DWORD64), 0);//二级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x1FC), &tempYGAddr, sizeof(DWORD64), 0);//三级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x0), &tempYGAddr, sizeof(DWORD64), 0);//四级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x608), &tempYGAddr, sizeof(DWORD64), 0);//五级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x38), &tempYGAddr, sizeof(DWORD64), 0);//六级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x8), &tempYGAddr, sizeof(DWORD64), 0);//七级偏移
            int WriteYG = 100; //写入体力数量
            WriteProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x6EC), &WriteYG, sizeof(WriteYG), 0);
        }
        if(wxsh){
            DWORD64 tempYGAddr = 0; //临时水壶地址

            ReadProcessMemory(GameProcessHandle, (LPVOID)((DWORD64)(ModuleHandle) + 0x00077238), &tempYGAddr, sizeof(DWORD64), 0); //一级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x10), &tempYGAddr, sizeof(DWORD64), 0);//二级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x1FC), &tempYGAddr, sizeof(DWORD64), 0);//三级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x0), &tempYGAddr, sizeof(DWORD64), 0);//四级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x5E0), &tempYGAddr, sizeof(DWORD64), 0);//五级偏移
            ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x110), &tempYGAddr, sizeof(DWORD64), 0);//六级偏移
            int WriteYG = 40; //写入水壶数量
            WriteProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x4C), &WriteYG, sizeof(WriteYG), 0);
        }
        if(yjks){
            qDebug("yjks=====");
            if(!yjksIsScan){
                yjksIsScan = true;
                // 示例特征码和掩码
                std::vector<BYTE> pattern = { 0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x80, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
                std::string mask = "xxxxxxxxxxxxxxxx"; // 掩码 'x' 表示完全匹配，'?' 表示通配符

                // 设置起始和结束地址
                uintptr_t startAddress = 0x7FFE00000000; // 起始地址
                uintptr_t endAddress = 0x7FFEFFFFFFFF;   // 结束地址
                // 查找特征码
                uintptr_t foundAddress = FindPattern(GameProcessHandle, startAddress, endAddress, pattern, mask);
                float newValue = 10.0f; //设置树木的值为10

                if (foundAddress != 0) {
                    // 将 uintptr_t 转换为 QString 并打印
                    qDebug() << "Address:" << QString("0x%1").arg(foundAddress, 0, 16);
                    WriteProcessMemory(GameProcessHandle, (LPVOID)foundAddress, &newValue, sizeof(newValue), nullptr);
                    qDebug("所有匹配地址值已修改为: 10.0");
                    yjksIsScan = true;
                } else {
                    qDebug("未找到匹配的地址！");
                    yjksIsScan = false;
                }
            }

        }
    }
    else{
       GameProcessID = 0;
       wxyg = false;
       ui->checkBox->setChecked(false);
    }
}

DWORD Widget::getGameProcessID(){
    return GameProcessID;
}
bool Widget::getFirst(){
    return first;
}
void Widget::setFirst(bool toFitst){
    first = toFitst;
}

void Widget::on_checkBox_stateChanged(int arg1)//无限阳光
{
    if(GameProcessID != 0){
        if(arg1 == 2){//被选中
            wxyg = true;
        }
        else{
            wxyg = false;
        }
    }
}

void Widget::on_checkBox_1_stateChanged(int arg1)//无限体力
{
    if(GameProcessID != 0){
        if(arg1 == 2){//被选中
            wxtl = true;
        }
        else{
            wxtl = false;
        }
    }
}

void Widget::on_checkBox_2_stateChanged(int arg1)//无限生命
{
    if(GameProcessID != 0){
        if(arg1 == 2){//被选中
            wxhp = true;
        }
        else{
            wxhp = false;
        }
    }
}

void Widget::on_checkBox_3_stateChanged(int arg1)//无限生命
{
    if(GameProcessID != 0){
        if(arg1 == 2){//被选中
            wxsh = true;
        }
        else{
            wxsh = false;
        }
    }
}

void Widget::on_checkBox_4_stateChanged(int arg1)//一键砍树
{
    if(GameProcessID != 0){
        if(arg1 == 2){//被选中
            yjks = true;
        }
        else{
            yjks = false;
        }
    }
}

void Widget::on_radioButton_clicked() //0速度
{
    if(GameProcessID != 0 && ModuleHandle != 0){
        DWORD64 tempYGAddr = 0; //临时地址
        ReadProcessMemory(GameProcessHandle, (LPVOID)((DWORD64)(ModuleHandle) + 0x00077238), &tempYGAddr, sizeof(DWORD64), 0); //一级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x10), &tempYGAddr, sizeof(DWORD64), 0);//二级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x1FC), &tempYGAddr, sizeof(DWORD64), 0);//三级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x0), &tempYGAddr, sizeof(DWORD64), 0);//四级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x2B8), &tempYGAddr, sizeof(DWORD64), 0);//五级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x130), &tempYGAddr, sizeof(DWORD64), 0);//六级偏移
        int WriteYG = 0; //写入速度
        WriteProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x4C), &WriteYG, sizeof(WriteYG), 0);
    }
}
void Widget::on_radioButton_2_clicked() //1加速
{
    if(GameProcessID != 0 && ModuleHandle != 0){
        DWORD64 tempYGAddr = 0; //临时地址
        ReadProcessMemory(GameProcessHandle, (LPVOID)((DWORD64)(ModuleHandle) + 0x00077238), &tempYGAddr, sizeof(DWORD64), 0); //一级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x10), &tempYGAddr, sizeof(DWORD64), 0);//二级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x1FC), &tempYGAddr, sizeof(DWORD64), 0);//三级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x0), &tempYGAddr, sizeof(DWORD64), 0);//四级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x2B8), &tempYGAddr, sizeof(DWORD64), 0);//五级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x130), &tempYGAddr, sizeof(DWORD64), 0);//六级偏移
        int WriteYG = 1065353216; //写入速度
        WriteProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x4C), &WriteYG, sizeof(WriteYG), 0);
    }
}
void Widget::on_radioButton_3_clicked() //5加速
{
    if(GameProcessID != 0 && ModuleHandle != 0){
        DWORD64 tempYGAddr = 0; //临时地址
        ReadProcessMemory(GameProcessHandle, (LPVOID)((DWORD64)(ModuleHandle) + 0x00077238), &tempYGAddr, sizeof(DWORD64), 0); //一级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x10), &tempYGAddr, sizeof(DWORD64), 0);//二级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x1FC), &tempYGAddr, sizeof(DWORD64), 0);//三级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x0), &tempYGAddr, sizeof(DWORD64), 0);//四级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x2B8), &tempYGAddr, sizeof(DWORD64), 0);//五级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x130), &tempYGAddr, sizeof(DWORD64), 0);//六级偏移
        int WriteYG = 1084227584; //写入速度
        WriteProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x4C), &WriteYG, sizeof(WriteYG), 0);
    }
}
void Widget::on_radioButton_4_clicked() //10加速
{
    if(GameProcessID != 0 && ModuleHandle != 0){
        DWORD64 tempYGAddr = 0; //临时地址
        ReadProcessMemory(GameProcessHandle, (LPVOID)((DWORD64)(ModuleHandle) + 0x00077238), &tempYGAddr, sizeof(DWORD64), 0); //一级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x10), &tempYGAddr, sizeof(DWORD64), 0);//二级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x1FC), &tempYGAddr, sizeof(DWORD64), 0);//三级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x0), &tempYGAddr, sizeof(DWORD64), 0);//四级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x2B8), &tempYGAddr, sizeof(DWORD64), 0);//五级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x130), &tempYGAddr, sizeof(DWORD64), 0);//六级偏移
        int WriteYG = 1092616192; //写入速度
        WriteProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x4C), &WriteYG, sizeof(WriteYG), 0);
    }
}
void Widget::on_radioButton_5_clicked() //20减速
{
    if(GameProcessID != 0 && ModuleHandle != 0){
        DWORD64 tempYGAddr = 0; //临时地址
        ReadProcessMemory(GameProcessHandle, (LPVOID)((DWORD64)(ModuleHandle) + 0x00077238), &tempYGAddr, sizeof(DWORD64), 0); //一级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x10), &tempYGAddr, sizeof(DWORD64), 0);//二级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x1FC), &tempYGAddr, sizeof(DWORD64), 0);//三级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x0), &tempYGAddr, sizeof(DWORD64), 0);//四级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x2B8), &tempYGAddr, sizeof(DWORD64), 0);//五级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x130), &tempYGAddr, sizeof(DWORD64), 0);//六级偏移
        int WriteYG = 1101004800; //写入速度
        WriteProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x4C), &WriteYG, sizeof(WriteYG), 0);
    }
}
void Widget::on_radioButton_6_clicked() //20减速
{
    if(GameProcessID != 0 && ModuleHandle != 0){
        DWORD64 tempYGAddr = 0; //临时地址
        ReadProcessMemory(GameProcessHandle, (LPVOID)((DWORD64)(ModuleHandle) + 0x00077238), &tempYGAddr, sizeof(DWORD64), 0); //一级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x10), &tempYGAddr, sizeof(DWORD64), 0);//二级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x1FC), &tempYGAddr, sizeof(DWORD64), 0);//三级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x0), &tempYGAddr, sizeof(DWORD64), 0);//四级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x2B8), &tempYGAddr, sizeof(DWORD64), 0);//五级偏移
        ReadProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x130), &tempYGAddr, sizeof(DWORD64), 0);//六级偏移
        int WriteYG = 1112014848; //写入速度
        WriteProcessMemory(GameProcessHandle, (LPVOID)(tempYGAddr + 0x4C), &WriteYG, sizeof(WriteYG), 0);
    }
}


void Widget::on_label_4_linkActivated(const QString &link) //GitHub超链接
{
    QDesktopServices::openUrl(QUrl(link));
}


void Widget::on_label_13_linkActivated(const QString &link) //哔哩哔哩超链接
{
    QDesktopServices::openUrl(QUrl(link));
}
